package com.example.zhou.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.test.TouchUtils;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.example.zhou.adapter.AlbumAdapter;
import com.example.zhou.adapter.Decoration.MyGirdDecoration;
import com.example.zhou.androidmusic.AlbumInfoActivity;
import com.example.zhou.androidmusic.R;
import com.example.zhou.filehelper.AlbumUtil;
import com.example.zhou.filehelper.FileToJson;
import com.example.zhou.help.ToActivityUtil;
import com.example.zhou.musicmodel.MusicBean;
import com.example.zhou.musicmodel.PalleteColor;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link AlbumListFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link AlbumListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AlbumListFragment extends Fragment implements AlbumAdapter.OnItemClick {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<Map<String, List<MusicBean>>> mapList = new ArrayList<>();
    private List<String> albumList = new ArrayList<>();
    private AlbumAdapter albumAdapter;
    private List<PalleteColor> palettelist = new ArrayList<>();


    public AlbumListFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment AlbumListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static AlbumListFragment newInstance() {

        return new AlbumListFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    /**
     * 获取文件中所有的音乐
     *
     * @return List<MusicBean>
     */
    private List<MusicBean> getJsonArray() {
        String musicInfo = FileToJson.read(getContext());
        if (TextUtils.isEmpty(musicInfo)) {
            FileToJson.writeToFile(getContext());
            musicInfo = FileToJson.read(getContext());
        }
        Log.e("music", musicInfo);
        return JSON.parseArray(musicInfo, MusicBean.class);
    }

    /**
     * 将所有的音乐按专辑分组
     */
    private List<Map<String, List<MusicBean>>> getList() {
        List<MusicBean> list = getJsonArray();
        List<Map<String, List<MusicBean>>> lmblist = new ArrayList<>();
        for (MusicBean musicBean : list) {
            List<MusicBean> list1 = new ArrayList<>();
            Map<String, List<MusicBean>> map = new HashMap<>();
            String album = musicBean.getAlbum();
            for (MusicBean musicBean1 : list) {
                if (musicBean1.getAlbum().contains(musicBean.getAlbum())) {
                    list1.add(musicBean);
                }
            }
            map.put(album, list1);
            /**筛选出未包含的专辑，然后插入*/
            if (!albumList.contains(album)) {
                albumList.add(album);
                lmblist.add(map);
            } else {
                Log.e("jjj", album);
            }

        }

        return lmblist;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_album_list, container, false);
        ButterKnife.bind(this, view);
        mapList = getList();
        //  getColorList(mapList, albumList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, LinearLayoutManager.VERTICAL, false);
        albumAdapter = new AlbumAdapter(getContext(), mapList, albumList, palettelist);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(albumAdapter);
        MyGirdDecoration myGirdDecoration = new MyGirdDecoration(10);
        recyclerView.addItemDecoration(myGirdDecoration);
        albumAdapter.setOnItemClick(this);
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
       /* if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }*/
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
       /* if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }*/
    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    @Override
    public void OnClick(View view, List<MusicBean> musicBeen, int postion) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("MUSICBEAN", (Serializable) musicBeen);
        ToActivityUtil.toNextActivity(getContext(), AlbumInfoActivity.class, bundle);
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }


}
